<script>
import { computed, defineComponent, reactive, ref, toRaw } from 'vue'
import * as Antd from 'ant-design-vue'
import ImportButton from '@Components/importButton'
import { isJSON } from '@Untils'
import { uniqueId } from 'lodash'

export default defineComponent({
  components: {
    ImportButton,
  },
  emits: ['result', 'close'],
  props: {
    upload_url: {
      type: String,
      default: () => '/file/uploadImg',
    },
    max: {
      type: Number,
      default: () => 100000,
    },
    record: {
      type: Array,
      default: () => {
        return []
      },
    },
  },
  setup(props, context) {
    let upload_url = computed(() => {
      return props.upload_url
    })

    let max = computed(() => {
      return props.max
    })

    let form_state = reactive({
      data: {
        picUrl: props.record,
      },
    })

    let record = computed(() => {
      return props.record
    })

    let loading = ref(false)
    let submitHandler = () => {
      // console.log(form_state.data.picUrl)

      //  console.log([...toRaw(form_state.data.picUrl)])
      loading.value = true
      context.emit('result', {
        list: [...toRaw(form_state.data.picUrl)],
        innerCallBack: (_result) => {
          loading.value = false
        },
      })
    }

    let cancelHandler = () => {
      context.emit('close')
    }

    return {
      upload_url,
      max,
      form_state,
      record,
      submitHandler,
      cancelHandler,
      loading,
    }
  },
  render() {
    return (
      <Antd.Row>
        <Antd.Col span={24}>
          <ImportButton
            upload_url={this.upload_url}
            attrs={{
              'list-type': 'picture-card',
              'show-upload-list': true,
              accept: 'image/*',
              multiple: true,
            }}
            max={this.max}
            {...(this.record.length > 0
              ? (() => {
                  //{ value: [{ url: record.value.picUrl, uid: -1 }] }

                  if (this.record && this.record.length > 0) {
                    let _arr = this.record
                    let _result = { value: [] }
                    _arr.map((x) => {
                      _result.value.push({ url: x, uid: uniqueId() })
                    })
                    return _result
                  }
                })()
              : {})}
            onComplete={(e) => {
              if (e.data == null) return
              this.form_state.data.picUrl.push(e.data)
            }}
            onRemove={(file) => {
              let url = ''
              if (file.data.response) {
                url = file.data.response.data
              } else {
                url = file.data.url
              }
              let _index = this.form_state.data.picUrl.findIndex(
                (x) => x == url
              )
              this.form_state.data.picUrl.splice(_index, 1)
            }}
          />
        </Antd.Col>

        <Antd.Col span={24}>
          <Antd.Space>
            <Antd.Button
              type="primary"
              onClick={() => {
                this.submitHandler()
              }}
              loading={this.loading}
            >
              确定
            </Antd.Button>
            <Antd.Button
              type="primary"
              danger
              onClick={this.cancelHandler}
              loading={this.loading}
            >
              取消
            </Antd.Button>
          </Antd.Space>
        </Antd.Col>
      </Antd.Row>
    )
  },
})
</script>
